﻿using System;
using Team4Mock.Entity;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Team4Mock.UserControl.Directorate
{
    public partial class AddDirectorate : System.Web.UI.UserControl
    {
        DirectorateEntity de = new DirectorateEntity();
        DepartmentEntity dd = new DepartmentEntity();
        OrganisationEntity oe = new OrganisationEntity();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Convert.ToString(Session["role"])))
                {
                    if (Session["role"].ToString().Equals("Admin"))
                    {
                        btnDel.Visible = true;
                    }
                    else
                    {
                        btnDel.Visible = false;
                    }
                }
                else
                {
                    btnDel.Visible = false;
                }
                dlNational.DataSource = oe.GetCountry();
                dlNational.DataBind();
                if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["Org"])))
                {

                }
                if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["s"])))
                {
                    try
                    {
                        int s = int.Parse(Request.QueryString["s"]);
                        dd.DirectorateID = s;
                        gvDepartment.DataSource = dd.GetDataById(dd);
                        gvDepartment.DataBind();

                        de.Directorateid = s;
                        DataTable dt = new DataTable();
                        dt = de.GetIDDirec(de);
                        txtDirecName.Text = dt.Rows[0][1].ToString();
                        txtDesShort.Text = dt.Rows[0][2].ToString();
                        idContact.Value = dt.Rows[0][3].ToString();
                        txtAdd1.Text = dt.Rows[0][4].ToString();
                        txtAdd2.Text = dt.Rows[0][5].ToString();
                        txtAdd3.Text = dt.Rows[0][6].ToString();
                        PostCode.Value = dt.Rows[0][7].ToString();
                        Town.Value = dt.Rows[0][8].ToString();
                        County.Value = dt.Rows[0][9].ToString();
                        dlNational.SelectedValue = dt.Rows[0][10].ToString();
                        hdidBus.Value = dt.Rows[0][11].ToString();
                        hdsic.Value = dt.Rows[0][12].ToString();
                        txtDesFull.Text = dt.Rows[0][13].ToString();
                        txtPhone.Text = dt.Rows[0][14].ToString();
                        txtFax.Text = dt.Rows[0][15].ToString();
                        txtWeb.Text = dt.Rows[0][16].ToString();
                        txtCharNum.Text = dt.Rows[0][17].ToString();
                        txtComNum.Text = dt.Rows[0][18].ToString();
                        if (dt.Rows[0][20].ToString() == "1")
                        {
                            isactive.Checked = true;
                        }
                        else
                        {
                            isactive.Checked = false;
                        }
                    }
                    catch
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "Error();", true);
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["Org"])))
            {
                if (txtDirecName.Text == "" || hdidBus.Value == "" || idContact.Value=="")
                {
                    tb.Text = "Input Mandatory Fields";
                }
                else
                {
                    try
                    {
                        string name = txtDirecName.Text,
                               shortdes = txtDesShort.Text,
                               bus = hdidBus.Value,
                               sic = hdsic.Value,
                               contactid = idContact.Value,
                               full = txtDesFull.Text,
                               orgid = Request.QueryString["Org"];
                        string add1 = "",
                               add2 = "",
                               add3 = "",
                               postcode = "",
                               town = "",
                               county = "",
                               country = "",
                               phone = "",
                               fax = "",
                               web = "",
                               charity = "",
                               company = "";
                        if (ckCOpy.Checked == true)
                        {
                            int idorg = int.Parse(Request.QueryString["Org"]);
                            de.OrganizationID = idorg;
                            DataTable dt = new DataTable();
                            dt = de.CopyOrganization(de);
                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                add1 = dt.Rows[0][0].ToString();
                                add2 = dt.Rows[0][1].ToString();
                                add3 = dt.Rows[0][2].ToString();
                                postcode = dt.Rows[0][3].ToString();
                                town = dt.Rows[0][4].ToString();
                                county = dt.Rows[0][5].ToString();
                                country = dt.Rows[0][6].ToString();
                                phone = dt.Rows[0][7].ToString();
                                fax = dt.Rows[0][8].ToString();
                                web = dt.Rows[0][9].ToString();
                                charity = dt.Rows[0][10].ToString();
                                company = dt.Rows[0][11].ToString();
                            }
                            de.DirectorateName = name;
                            de.DirectorateShortDescription = shortdes;
                            de.ContactID = int.Parse(contactid);
                            de.AddressLine1 = add1;
                            de.AddressLine2 = add2;
                            de.AddressLine3 = add3;
                            de.Postcode = postcode;
                            de.Town = town;
                            de.County = county;
                            de.Country = country;
                            de.TypeOfBusiness = bus;
                            de.SICCode = sic;
                            de.DirectorateFullDescription = full;
                            de.PhoneNumber = phone;
                            de.Fax = fax;
                            de.WebAddress = web;
                            de.CharityNumber = charity;
                            de.CompanyNumber = company;
                            de.OrganizationID = int.Parse(orgid);
                            if (isactive.Checked == true)
                            {
                                de.IsActive = 1;
                            }
                            else
                            {
                                de.IsActive = 0;
                            }
                            de.Insert(de);
                            tb.Text = "Insert success";
                        }
                        else
                        {
                            if (PostCode.Value == "" || txtAdd1.Text == "")
                            {
                                tb.Text = "Select Post Code and Address Line 1";
                            }
                            else
                            {
                                postcode = PostCode.Value;
                                town = Town.Value;
                                county = County.Value;
                                country = dlNational.SelectedValue;

                                add1 = txtAdd1.Text;
                                add2 = txtAdd2.Text;
                                add3 = txtAdd3.Text;
                                phone = txtPhone.Text;
                                fax = txtFax.Text;
                                web = txtWeb.Text;
                                charity = txtCharNum.Text;
                                company = txtComNum.Text;
                                de.DirectorateName = name;
                                de.DirectorateShortDescription = shortdes;
                                de.ContactID = int.Parse(contactid);
                                de.AddressLine1 = add1;
                                de.AddressLine2 = add2;
                                de.AddressLine3 = add3;
                                de.Postcode = postcode;
                                de.Town = town;
                                de.County = county;
                                de.Country = country;
                                de.TypeOfBusiness = bus;
                                de.SICCode = sic;
                                de.DirectorateFullDescription = full;
                                de.PhoneNumber = phone;
                                de.Fax = fax;
                                de.WebAddress = web;
                                de.CharityNumber = charity;
                                de.CompanyNumber = company;
                                de.OrganizationID = int.Parse(orgid);
                                if (isactive.Checked == true)
                                {
                                    de.IsActive = 1;
                                }
                                else
                                {
                                    de.IsActive = 0;
                                }
                                de.Insert(de);
                                tb.Text = "Insert success";
                            }
                        }
                    }
                    catch
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "Error();", true);
                    }
                }

            }
            if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["s"])))
            {
                if (txtDirecName.Text == "" ||hdidBus.Value==""||idContact.Value=="")
                {
                    tb.Text = "Input Mandatory Fields";
                }
                else
                {
                    try
                    {
                        string name = txtDirecName.Text,
                               shortdes = txtDesShort.Text,
                               bus = hdidBus.Value,
                               sic = hdsic.Value,
                               contactid = idContact.Value,
                               full = txtDesFull.Text,
                               orgid = Request.QueryString["ID"];
                        string add1 = "",
                               add2 = "",
                               add3 = "",
                               postcode = "",
                               town = "",
                               county = "",
                               country = "",
                               phone = "",
                               fax = "",
                               web = "",
                               charity = "",
                               company = "";
                        if (ckCOpy.Checked == true)
                        {
                            int idorg = int.Parse(Request.QueryString["ID"]);
                            de.OrganizationID = idorg;
                            DataTable dt = new DataTable();
                            dt = de.CopyOrganization(de);
                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                add1 = dt.Rows[0][0].ToString();
                                add2 = dt.Rows[0][1].ToString();
                                add3 = dt.Rows[0][2].ToString();
                                postcode = dt.Rows[0][3].ToString();
                                town = dt.Rows[0][4].ToString();
                                county = dt.Rows[0][5].ToString();
                                country = dt.Rows[0][6].ToString();
                                phone = dt.Rows[0][7].ToString();
                                fax = dt.Rows[0][8].ToString();
                                web = dt.Rows[0][9].ToString();
                                charity = dt.Rows[0][10].ToString();
                                company = dt.Rows[0][11].ToString();
                            }
                            de.Directorateid = int.Parse(Request.QueryString["s"]);
                            de.DirectorateName = name;
                            de.DirectorateShortDescription = shortdes;
                            de.ContactID = int.Parse(contactid);
                            de.AddressLine1 = add1;
                            de.AddressLine2 = add2;
                            de.AddressLine3 = add3;
                            de.Postcode = postcode;
                            de.Town = town;
                            de.County = county;
                            de.Country = country;
                            de.TypeOfBusiness = bus;
                            de.SICCode = sic;
                            de.DirectorateFullDescription = full;
                            de.PhoneNumber = phone;
                            de.Fax = fax;
                            de.WebAddress = web;
                            de.CharityNumber = charity;
                            de.CompanyNumber = company;
                            de.OrganizationID = int.Parse(orgid);
                            if (isactive.Checked == true)
                            {
                                de.IsActive = 1;
                            }
                            else
                            {
                                de.IsActive = 0;
                            }
                            de.Update(de);
                            tb.Text = "Update success";
                        }
                        else
                        {
                            if (PostCode.Value == "" || txtAdd1.Text == "")
                            {
                                tb.Text = "Select Post Code and Address Line 1";
                            }
                            else
                            {
                                postcode = PostCode.Value;
                                town = Town.Value;
                                county = County.Value;
                                country = dlNational.SelectedValue;

                                add1 = txtAdd1.Text;
                                add2 = txtAdd2.Text;
                                add3 = txtAdd3.Text;
                                phone = txtPhone.Text;
                                fax = txtFax.Text;
                                web = txtWeb.Text;
                                charity = txtCharNum.Text;
                                company = txtComNum.Text;
                                de.Directorateid = int.Parse(Request.QueryString["s"]);
                                de.DirectorateName = name;
                                de.DirectorateShortDescription = shortdes;
                                de.ContactID = int.Parse(contactid);
                                de.AddressLine1 = add1;
                                de.AddressLine2 = add2;
                                de.AddressLine3 = add3;
                                de.Postcode = postcode;
                                de.Town = town;
                                de.County = county;
                                de.Country = country;
                                de.TypeOfBusiness = bus;
                                de.SICCode = sic;
                                de.DirectorateFullDescription = full;
                                de.PhoneNumber = phone;
                                de.Fax = fax;
                                de.WebAddress = web;
                                de.CharityNumber = charity;
                                de.CompanyNumber = company;
                                de.OrganizationID = int.Parse(orgid);
                                if (isactive.Checked == true)
                                {
                                    de.IsActive = 1;
                                }
                                else
                                {
                                    de.IsActive = 0;
                                }
                                de.Update(de);
                                tb.Text = "Update success";
                            }
                        }
                    }
                    catch
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "Error();", true);
                    }
                }
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["Org"])))
            {
                Response.Redirect("/FormView/Organisation.aspx?ID=" + Request.QueryString["Org"]);
            }
            if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["ID"])))
            {
                Response.Redirect("/FormView/Organisation.aspx?ID=" + Request.QueryString["ID"]);
            }
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            Response.Redirect("/FormView/Department.aspx?s=" + Request.QueryString["s"] + "&ID=" + Request.QueryString["ID"]);
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
                int id = int.Parse(Request.QueryString["s"]);
                de.Directorateid = id;
                de.Del(de);
                Response.Redirect("/FormView/Organisation.aspx?ID=" + Request.QueryString["ID"]);
            }
            catch
            {
                tb.Text = "Error while being processed";
            }

        }
    }
}